package encaps.action;

import encaps.resources.Labels;
import encaps.utils.IOHelper;
import encaps.utils.SystemException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

public class ActionContext {

    public static final String USER_LOGIN = "user";
    public static final String USER_ROLE = "role";
    private static final String MULTIPART = "multipart";
    private static final String CONTENT_TYPE = "content-type";
    private HttpServletRequest request;
    private Map<String, String> parameters;
    private boolean multipart;

    public ActionContext(HttpServletRequest request) {
        this.request = request;
        multipart = isMultipart();
        if (multipart) {
            parameters = new HashMap<String, String>();
        }
    }

    public void setUserRole(String userRole) {
        request.getSession().setAttribute(USER_ROLE, userRole);
    }

    public String getUserRole() {
        return (String) request.getSession().getAttribute(USER_ROLE);
    }

    public void setUserLogin(String userLogin) {
        request.getSession().setAttribute(USER_LOGIN, userLogin);
    }

    public String getUserLogin() {
        return (String) request.getSession().getAttribute(USER_LOGIN);
    }

    public String getTextParameter(String name) throws SystemException {
        InputStream in = null;
        Scanner scanner = null;
        try {
            if (multipart) {
                String parameter = parameters.get(name);
                if (parameter != null) {
                    return parameter;
                }
                Part part = request.getPart(name);
                if (part == null) {
                    return null;
                }
                in = part.getInputStream();
                scanner = new Scanner(in);
                if (scanner.hasNext()) {
                    parameter = scanner.nextLine();
                    parameters.put(name, parameter);
                    return parameter;
                } else {
                    return "";
                }
            } else {
                return request.getParameter(name);
            }
        } catch (Exception ex) {
            String message = String.format(Labels.getLabel(Labels.CANNOT_READ_PARAMETER_MESSAGE_PATTERN), name);
            throw new SystemException(message, ex);
        } finally {
            IOHelper.closeStream(in);
            IOHelper.closeScanner(scanner);
        }
    }

    public String getImageType(String fileInputName) throws SystemException {
        try {
            Part part = request.getPart(fileInputName);
            String type = part.getHeader(CONTENT_TYPE);
            if (type == null) {
                return null;
            }
            return type.substring(type.lastIndexOf("/") + 1);
        } catch (Exception ex) {
            String message = String.format(Labels.getLabel(Labels.CANNOT_READ_PARAMETER_MESSAGE_PATTERN), fileInputName);
            throw new SystemException(message, ex);
        }
    }

    public InputStream getFileInputStream(String fileInputName) throws SystemException {
        try {
            Part part = request.getPart(fileInputName);
            return part.getInputStream();
        } catch (Exception ex) {
            String message = Labels.getLabel(Labels.CANNOT_READ_THE_FILE_MESSAGE);
            throw new SystemException(message, ex);
        }
    }

    public String getActionName() throws SystemException {
        return getTextParameter(Action.ACTION);
    }

    private boolean isMultipart() {
        String contentType = request.getContentType();
        if (contentType == null) {
            return false;
        }
        if (contentType.contains(MULTIPART)) {
            return true;
        } else {
            return false;
        }
    }
}
